package com.example.petstoreservice.controller;

import com.example.petstoreservice.entity.Result;
import com.example.petstoreservice.entity.Supplier;
import com.example.petstoreservice.entity.User;
import com.example.petstoreservice.mapper.UserMapper;
import com.example.petstoreservice.service.SupplierService;
import com.example.petstoreservice.service.UserService;
import com.example.petstoreservice.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private UserService userService;
    @Autowired
    private SupplierService supplierService;
    @PostMapping("/reset/{username}")
    public Result<?> reset(@PathVariable String username){
        String resetPwd= Md5Util.getMD5String("123456");
        if(userService.updatePassword(username,resetPwd)==2){
            return Result.success("密码重置成功");
        }else {
            return Result.fail();
        }
    }
    @PostMapping("/setEnable/{username}/{value}")
    public Result<?> setEnable(@PathVariable String username,@PathVariable int value){
        User user=userService.findByUsername(username);
        user.setEnable(value);
        if(userService.updateUser(user)){
            return Result.success("设置成功");
        }else {
            return Result.fail();
        }
    }
    @GetMapping("/deleteSupplier/{id}")
    public Result<?> deleteSupplier(@PathVariable Long id)
    {
        boolean y=supplierService.delete(id);
        if(y){
            return Result.success("删除成功!");
        }else{
            return Result.fail("删除失败!");
        }
    }
    @PostMapping("/insertSupplier")
    public Result<?> insertSupplier(@RequestBody Supplier supplier){
        if(supplierService.insert(supplier)){
            return Result.success("添加成功");
        }else{
            return Result.fail("新增失败!");
        }
    }


}
